import React from 'react';

import {Button} from 'antd';

import {get, post} from '../../../logic/api';

import ObjectList from '../../components/ObjectList';
const Operation = ObjectList.Operation;

import QiniuUploadimage from '../../components/QiniuUploadimage';

export default class Edit extends React.Component {

  state = {
    parent:0
  }

  dataIndex = [
    {
      subtitle:'中文名称',
      name:'cname',
      type:'textarea'
    },{
      subtitle:'英文名称',
      name:'ename',
      type:'textarea'
    },{
      subtitle:'描述',
      name:'describe',
      type:'textarea'
    },{
      subtitle:'图片',
      name:'image',
      type:'img'
    }

  ];

  componentWillMount(){
  }

  render(){
    var finaldataIndex = this.dataIndex.map(v=>v);
    if (this.props.record.type === 'step'){
      //步骤没有图片
      finaldataIndex.splice(3, 1);
    }
    else if (this.props.record.type === 'stepcontent'){
      //步骤内容没有详情
      finaldataIndex.splice(2, 1);
    }

    return <Operation
      width = {800}
      text = {this.props.text?this.props.text:"编辑"}
      dataIndex = {finaldataIndex}
      record = {this.props.record}
      fetchData = {async ()=>{
        if (this.props.fetchData){
          return await this.props.fetchData(this.props.record);
        }

        var ret = await get('/admin/custom/info/' + this.props.record.type + '/' + this.props.record.id);

        return ret;
      }}
      onOk = {async (r, f) => {

        if (r.customId){
          f.customId = r.customId
        }else if (r.customstepId){
          f.customstepId = r.customstepId;
        }

        var ret = await post('/admin/custom/upsert/' + r.type + '/' + r.id, f);

        if (ret.ok){
          if (this.props.reload){
            this.props.reload();
          }
        }
        
        return ret;
      }}
      onCancel = {async ()=>{
      }}
    />
  }
}